#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "stu.h"

int main() {
    ALGraph G;
    LISTS list;
    list.length = 0;
    VertexType V[21];
    KeyType VR[100][2];
    char FileName[20];
    int i = 0, op, len, k;
    int *L;
    KeyType u, v, w;
    ArcNode *p;
    VertexType value;
    printf("请输入您的选择 1 代表多表 2 代表图 0 代表退出\n");
    scanf("%d", &op);
    while (op) {
        if (op == 1) {
            help_list();
            printf("请输入您的选择\n");
            scanf("%d", &op);
            while (op) {
                switch (op) {
                case 1:
                    printf("请输入图的名称\n");
                    scanf("%s", FileName);
                    i = CreateLists(list, FileName);
                    if (i == INFEASIBLE)
                        printf("多表已满无法加入\n");
                    else if (i == ERROR)
                        printf("创建失败\n");
                    else if (i == OK)
                        printf("创建成功\n");
                    break;
                case 2:
                    printf("请输入图的名称\n");
                    scanf("%s", FileName);
                    i = Deletelist(list, FileName);
                    if (i == INFEASIBLE)
                        printf("多表为空\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 3:
                    printf("请输入图的名称\n");
                    scanf("%s", FileName);
                    i = Checklist(list, FileName);
                    if (i == INFEASIBLE)
                        printf("查找有误\n");
                    else
                        printf("您要查找的%s的位置是%d\n", FileName, i + 1);
                    break;
                case 4:
                    help_list();
                    break;
                default:
                    printf("您的输入有误,请重新输入\n");
                    break;
                }
                printf("请输入您的选择\n");
                scanf("%d", &op);
            }
        } else if (op == 2) {
            printf("请输入您要操作的邻接表\n");
            scanf("%s", FileName);
            i = Checklist(list, FileName);
            if (i == -1){
             
                printf("输入数据有误,请重新输入\n");
                continue;
            }
            help_G();

            G = list.elem[i].G;
            printf("请输入您的选择\n");
            scanf("%d", &op);
            while (op) {
                switch (op) {
                case 1:
                    printf("请输入数据\n");
                    i = 0;
                    do {
                        scanf("%d%s", &V[i].key, V[i].others);
                    } while (V[i++].key != -1);
                    i = 0;
                    do {
                        scanf("%d%d", &VR[i][0], &VR[i][1]);
                    } while (VR[i++][0] != -1);
                    i = CreateCraph(G, V, VR);
                    if (i == ERROR)
                        printf("数据有误\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 2:
                    i = DestroyGraph(G);
                    if (i == OK)
                        printf("成功\n");
                    else if (i == INFEASIBLE)
                        printf("邻接表有误\n");
                    break;
                case 3:
                    printf("请输入关键字\n");
                    scanf("%d", &u);
                    i = LocateVex(G, u);
                    if (i == -1)
                        printf("不存在这个结点\n");
                    else {
                        printf("成功\n");
                        visit(G.vertices[i].data);
                    }
                    break;
                case 4:
                    printf("请输入关键字 及结点数据\n");
                    scanf("%d%d%s", &u, &value.key, value.others);
                    i = PutVex(G, u, value);
                    if (i == ERROR)
                        printf("查找失败或关键字不唯一\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 5:
                    printf("请输入结点关键字\n");
                    scanf("%d", &u);
                    i = FirstAdjVex(G, u);
                    if (i == -1)
                        printf("数据有误\n");
                    else {
                        printf("查找成功\n");
                        printf("第一邻接点位序是  %d,第一邻接点是  %d %s\n", i, G.vertices[i].data.key, G.vertices[i].data.others);
                    }
                    break;
                case 6:
                    printf("请输入两个邻接点数据的关键字\n");
                    scanf("%d%d", &v, &w);
                    i = NextAdjVex(G, v, w);
                    if (i == -1)
                        printf("结点数据有误\n");
                    else {
                        printf("成功\n");
                        printf("下一邻接点位序是%d,下一邻接点是%d %s\n", i, G.vertices[i].data.key, G.vertices[i].data.others);
                    }
                    break;
                case 7:
                    printf("请输入结点数据\n");
                    scanf("%d%s", &value.key, value.others);
                    i = InsertVex(G, value);
                    if (i == ERROR)
                        printf("插入失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 8:
                    printf("请输入结点关键字\n");
                    scanf("%d", &v);
                    i = DeleteVex(G, v);
                    if (i == ERROR)
                        printf("删除失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 9:
                    printf("请输入弧两端的端点关键字\n");
                    scanf("%d%d", &v, &w);
                    i = InsertArc(G, v, w);
                    if (i == ERROR)
                        printf("增加失败\n");
                    else if (i == OK)
                        printf("插入成功\n");
                    break;

                case 10:
                    printf("请输入要删除的弧的两个结点关键字\n");
                    scanf("%d%d", &v, &w);
                    i = DeleteArc(G, v, w);
                    if (i == ERROR)
                        printf("数据有误删除失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 11:
                    i = DFSTraverse(G, visit);
                    if (i == ERROR)
                        printf("邻接表为空\n");
                    if (i == OK)
                        printf("成功\n");
                    break;
                case 12:
                    i = BFSTraverse(G, visit);
                    if (i == OK)
                        printf("成功\n");
                    break;
                case 13:
                    printf("请输入文件名称\n");
                    scanf("%s", FileName);
                    i = SaveGraph(G, FileName);
                    if (i == ERROR)
                        printf("文件打开失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 14:
                    printf("请输入文件名称\n");
                    scanf("%s", FileName);
                    i = LoadGraph(G, FileName);
                    if (i == ERROR)
                        printf("文件打开失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 15:
                    printf("请输入结点关键字以及距离\n");
                    scanf("%d%d", &v, &k);
                    L = VerticesSetLessThanK(G, v, k);
                    for (int i = 1; i <= L[0]; i++) {
                        visit(G.vertices[L[i]].data);
                    }
                    printf("\n");
                    printf("成功\n");
                    break;
                case 16:
                    i = ConnectedComponentsNums(G);
                    printf("图的连通分量是%d\n", i);
                    break;
                case 17:
                    i = Gtravel(G);
                    if (i == ERROR)
                        printf("邻接表数据有误\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 18:
                    help_G();
                    break;
                default:
                    printf("输入数据有误请重新输入\n");
                    break;
                }

                printf("请输入您的选择\n");
                scanf("%d", &op);
            }
        }
        printf("请输入您的选择 1 代表多表 2 代表图 0 代表退出\n");
        scanf("%d", &op);
    }
    return 1;
}
